import java.util.Scanner;

public class Demo5 {
    private static Object locker = new Object();

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            synchronized (locker){
                System.out.println("t1 wait之前");
                try {
                    locker.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("t1 wait之后");
            }
        });
        Thread t2 = new Thread(() -> {
            synchronized (locker){
                System.out.println("t2 wait之前");
                try {
                    locker.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("t2 wait之后");
            }
        });
        Thread t3 = new Thread(() -> {
            synchronized (locker){
                System.out.println("t3 wait之前");
                try {
                    locker.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("t3 wait之后");
            }
        });
        Thread t4 = new Thread(() -> {
            Scanner scanner = new Scanner(System.in);
            scanner.next(); //此处是通过这个next构造一个“阻塞”的状态
            System.out.println("t4 notifyAll之前");
            synchronized (locker) {
                locker.notifyAll();
            }
            System.out.println("t4 notifyAll之后");
        });
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
}
